var repayDetailData,repayTotal=0;
$(document).ready(function() {
	//初始化其他
	initOther();
});

var initOther = function(){
	//查询详情
	$.ajax({
		url:'sys/LoanExt/findRepaymentDetailForSettleAccounts',
		data:{'queryProxy[id]':_param_id},
		type:'post',
		dataType:'json',
		async:false,
		success:function(result){
			if(typeof(result)!='undefined'){
				repayDetailData = result;
			}
		},
		complete:function(){
		}
	});
//	//设置过期查询条件
	var array = new Array;
	array.push({name:'beanProxy[enable]',value:0});
	array.push({name:'beanProxy[loanId]',value:loanId});
	array.push({name:'beanProxy[repaymentId]',value:repaymentId});
	array.push({name:'beanProxy[id_NotEqualTo]',value:_param_id});
	array.push({name:'beanProxy[deadlineNumber_LessThan]',value:repayDetailData.deadlineNumber});
	array.push({name:'isNonPage',value:1});
	array.push({name:'beanProxy[repayStatus_In]',value:'3,4'});
	initLastEnterprise(array, function (data, display, row) {
		return data;
	});
	
	

};



var initHtmlRepaymentDetailSpan = function(data,sign){
	var url = 'sys/LoanExt/findRepaymentDetailForSettleAccounts?queryProxy[id]='+_param_id;
	_set_lable_text('#currentRepayDetail span ','name',url,false,data,function(value,name){
		var theOne = $('#'+sign+' span[name="'+name+'"]'),expired=$('#expired'+name);
		if(typeof(theOne)!='undefined' && typeof(expired)!='undefined'){
			var expiredValue = expired.text();
			if(_is_empty(expiredValue)){
				expiredValue = '0';
			}
			var sum = parseInt(value,10) + parseInt(expiredValue,10);
			theOne.text(sum);
		}
	});
};


var initLastEnterprise  = function(array,planTotal){
	if(typeof(planTotal)=='undefined'){
		planTotal=	function (data, display, row) {
			return data;
		};
	};
	$('#lastEnterprise').dataTable({
		"bProcessing" : true, //DataTables载入数据时，是否显示‘进度’提示
		"bServerSide" : true, //是否启动服务器端数据导入
		"bStateSave" : false, //是否打开客户端状态记录功能,此功能在ajax刷新纪录的时候不会将个性化设定回复为初始化状态
		"bJQueryUI" : true, //是否使用 jQury的UI theme
		//"aLengthMenu" : [5, 10, 15], //更改显示记录数选项
		"iDisplayLength" : 10, //默认显示的记录数
		"bAutoWidth" : true, //是否自适应宽度 update
		"bScrollCollapse" : false, //是否开启DataTables的高度自适应，当数据条数不够分页数据条数的时候，插件高度是否随数据条数而改变
		"bPaginate" : false, //是否显示（应用）分页器
		"bInfo" : false, //是否显示页脚信息，DataTables插件左下角显示记录数
		"sPaginationType" : "full_numbers", //详细分页组，可以支持直接跳转到某页
		"bSort" : true, //是否启动各个字段的排序功能
		"aaSorting" : [[0, "ASC"]], //默认的排序方式，第3列，升序排列
		"bFilter" : false, //是否启动过滤、搜索功能
		"sPaginationType": "full_numbers",
		"sScrollX": '100%',
		'destroy':true,
		"sScrollXInner": 900,
		"sAjaxSource":"sys/LoanRepaymentDetail/list",
		"fnServerData": function( sSource, aoData, fnCallback ) {
			$.merge(aoData,array);//合并
			$.unique(aoData);//去重
			$.ajax( {
				type: "POST", 
				url: sSource, 
				cache : false,
				dataType : "json",
				data : aoData, 
				success: function(resp) {
					if(resp && resp.state == 200){
						fnCallback(resp.datagrid);
						return false;
					}
					fnCallback(null);
				}
			});
		},  //自定义数据获取函数
		"aoColumns" : [ {
			"mDataProp" : "deadlineNumber",
			"sTitle" : "期数",
			"sDefaultContent" : "",
			"sClass" : "center",
			"bSortable": false
		},{
			"mDataProp" : "planCapital",
			"sTitle" : "本金",
			"sDefaultContent" : "",
			"sClass" : "center",
			"bSortable": false
		},{
			"mDataProp" : "planInterest",
			"sTitle" : "利息",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "planHandingFee",
			"sTitle" : "手续费",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center"
		},{
			"mDataProp" : "planInspectFee",
			"sTitle" : "调查费",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center"
		},{
			"mDataProp" : "planManageFee",
			"sTitle" : "管理费",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center"
		},{
			"mDataProp" : "planOverdueFee",
			"sTitle" : "滞纳金",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center"
		},{
			"mDataProp" : "planBreachFee",
			"sTitle" : "违约金",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center"
		},{
			"mDataProp" : "planCollectionFee",
			"sTitle" : "催收费",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center"
		},{
			"mDataProp" : "planLegalFee",
			"sTitle" : "法诉费",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center"
		},{
			"mDataProp" : "planTotal",
			"sTitle" : "总计",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender":planTotal
		}
		],
		"oLanguage": {
			"sUrl":top.basePath+"static_resources/language/lang-cn.txt" //这个是国际化，也就是汉化的各种文字数据源，写对url就行了
		},
		"footerCallback": function (tfoot, row, data, start, end, display ) {
			var api = this.api(), data;
			var intVal = function (i) {
				return typeof i === 'string' ?
						i.replace(/[\$,]/g, '')*1 :
							typeof i === 'number' ?
									i : 0;
			};

			pageTotal1 = api.column(1, { page: 'current'}).data().reduce( function (a, b) {
				return intVal(a) + intVal(b);
			}, 0);
			$('#expiredplanCapital').text(pageTotal1);

			pageTotal2 = api.column(2, { page: 'current'}).data().reduce( function (a, b) {
				return intVal(a) + intVal(b);
			}, 0);
			$('#expiredplanInterest').text(pageTotal2);

			pageTotal3 = api.column(3, { page: 'current'}).data().reduce( function (a, b) {
				return intVal(a) + intVal(b);
			}, 0);
			$('#expiredplanHandingFee').text(pageTotal3);

			pageTotal4 = api.column(4, { page: 'current'}).data().reduce( function (a, b) {
				return intVal(a) + intVal(b);
			}, 0);
			$('#expiredplanInspectFee').text(pageTotal4);

			pageTotal5 = api.column(5, { page: 'current'}).data().reduce( function (a, b) {
				return intVal(a) + intVal(b);
			}, 0);
			$('#expiredplanManageFee').text(pageTotal5);

			pageTotal6 = api.column(6, { page: 'current'}).data().reduce( function (a, b) {
				return intVal(a) + intVal(b);
			}, 0);
			$('#expiredplanOverdueFee').text(pageTotal6);

			pageTotal7 = api.column(7, { page: 'current'}).data().reduce( function (a, b) {
				return intVal(a) + intVal(b);
			}, 0);
			$('#expiredplanBreachFee').text(pageTotal7);

			pageTotal8 = api.column(8, { page: 'current'}).data().reduce( function (a, b) {
				return intVal(a) + intVal(b);
			}, 0);
			$('#expiredplanCollectionFee').text(pageTotal8);

			pageTotal9 = api.column(9, { page: 'current'}).data().reduce( function (a, b) {
				return intVal(a) + intVal(b);
			}, 0);
			$('#expiredplanLegalFee').text(pageTotal9);

			pageTotal10 = api.column(10, { page: 'current'}).data().reduce( function (a, b) {
				return intVal(a) + intVal(b);
			}, 0);
			$('#expiredplanTotal').text(pageTotal10);
			initHtmlRepaymentDetailSpan(repayDetailData,'currentRepayDetailSum');
		}
	});

};
